<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PlayerName
 *
 * @author Laszlo.Pokoradi
 */
class PlayerName {
  const FIRSTNAME_XPATH = "./first[1]";
  const LASTNAME_XPATH = "./last[1]";
  const NICKNAME_XPATH = "./nick[1]";

  private $firstName;
  private $lastName;
  private $nickName;
  private $db;

  public function __construct($database) {
    $this->db = $database;

    $this->firstName = "";
    $this->lastName = "";
    $this->nickName = "";
  }

  public function loadFromNode(DOMElement $docNode) {
    $doc = $docNode->ownerDocument;

    $xPath = new DOMXPath($doc);

    $qResult = $xPath->query(PlayerName::FIRSTNAME_XPATH,$docNode);
    $this->firstName = $qResult->item(0)->nodeValue;

    $qResult = $xPath->query(PlayerName::LASTNAME_XPATH,$docNode);
    $this->lastName = $qResult->item(0)->nodeValue;

    $qResult = $xPath->query(PlayerName::NICKNAME_XPATH,$docNode);
    $this->nickName = $qResult->item(0)->nodeValue;
  }

  public function toString() {
    $str = "{$this->firstName} ";
    if ($this->nickName) {
      $str.= "\"{$this->nickName}\" ";
    }
    $str .= "{$this->lastName}";

    return $str;
  }

  public function insertNewToDB($playerId) {
    $this->db->set('playerId',$playerId);
    $this->db->set('firstName',$this->firstName);
    $this->db->set('nickName',$this->nickName);
    $this->db->set('lastName',$this->lastName);
    $query = $this->db->insert('playername');

    if (!$query) {
      log_message('error', 'Error during playername inserttion!');
      return false;
    }

    return $this->db->insert_id();
  }

  public function amendOldInDB($nameId) {
    $this->db->where('id',$nameId);
    $query = $this->db->get('playername');

    if (!$query) {
      log_message('error','Error during playername amendment!');
    } else {
      $tRow = $query->row();
      $oldId = $tRow->id;
      $playerId = $tRow->playerId;

      if ($tRow->firstName!=$this->firstName
          || $tRow->nickName!=$this->nickName
          || $tRow->lastName!=$this->lastName) {
        $newId = $this->insertNewToDB($playerId);

        $this->db->set('playerId', $playerId);
        $this->db->set('table','playername');
        $this->db->set('oldId',$oldId);
        $this->db->set('newId',$newId);
        $this->db->set('insertTime',date('Y-m-d H:i:s'));
        $query = $this->db->insert('playerhistory');

        if (!$query) {
          log_message('error','Playername amend was unsuccesful!');
        } else {
          return $newId;
        }
      }
    }
    return $oldId;
  }

  public function loadFromDB($nameId) {
    $this->db->where('id',$nameId);
    $query = $this->db->get('playername');

    if (!$query || $query->num_rows()==0) {
      log_message('error', 'PlayerName ('.$nameId.') not found in the database!');
    } else {
      $result = $query->row();
      $this->firstName = $result->firstName;
      $this->nickName = $result->nickName;
      $this->lastName = $result->lastName;
    }
  }
}
?>
